<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div{
            position: absolute;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: pink;
        }
        span{
            position: absolute;
            left:0;
            top: 200px;
            width: 150px;
            height: 150px;
            background-color: yellow;
        }
    </style>
</head>
<div></div>
<span>好好好</span>
<body>
    <script>
        //obj是目标对象 target是目标位置
        function animate(obj,target){
            var timer=setInterval(function() {   //获取是通过offsetLeft获取的
            if(obj.offsetLeft>target){
                //停止动画,本质是停止定时器
                clearInterval(timer);
            }
            obj.style.left=obj.offsetLeft+1+'px';    
        }, 30);
        }
        var div=document.querySelector('div');
        var span=document.querySelector('span');
        animate(div,300); //调用函数
        animate(span,200);
    </script>
</body>
</html>